package Day2025_03_06;

import java.util.PriorityQueue;

class MedianFinder {

    PriorityQueue<Integer> left;
    PriorityQueue<Integer> right;

    public MedianFinder() {
        right = new PriorityQueue<>((a, b) -> a - b);
        left = new PriorityQueue<>((a, b) -> b - a);
    }

    public void addNum(int num) {
        if (left.size() == right.size()) {
            if (left.isEmpty() || num <= left.peek()) {
                left.offer(num);
            } else {
                right.offer(num);
                left.offer(right.poll());
            }
        } else {
            if (num <= left.peek()) {
                right.offer(left.poll());
                left.offer(num);
            } else {
                right.offer(num);
            }
        }
    }

    public double findMedian() {
        if (left.size() == right.size()) return (left.peek() + right.peek()) / 2.0;
        else return left.peek() * 1.0;
    }
}

public class LeetCode295 {
    public static void main(String[] args) {
        MedianFinder medianFinder = new MedianFinder();
        medianFinder.addNum(1);
        medianFinder.addNum(2);
        System.out.println(medianFinder.findMedian());
    }
}
